<?php
class Fax extends MY_Controller{

	function __construct()
	{
		parent::__construct();
		if(!$this->admin) redirect('admin990/login');
	}

	function index()
	{

		$data['title'] = "FAX";
		$data['queryResult'] = $this->fax_data();
		$this->load->view('admin/header',$data);
		$this->load->view('admin/fax/index');

	}
	
	
	function proprety()
	{
		if($_POST)
		{
			$fax_account = fax_account::find_by_status('inuse');
			$fax_account->username = $_POST['username'];
			$fax_account->password = $_POST['password'];
			if($fax_account->save()){
				$data['msg'] = 'Les changements ont été enregistré.';
			}else{
				$data['msg'] = 'Il y a une erreur.';
			}
			
			$data['title'] = "FAX";
			$data['fax_account'] = $fax_account;
			$this->load->view('admin/header',$data);
			$this->load->view('admin/fax/proprety');
		}else{
		
			$data['title'] = "FAX";
			$data['fax_account'] = $this->account_inuse();
			$this->load->view('admin/header',$data);
			$this->load->view('admin/fax/proprety');
		}
		
	}
	
	
	private function fax_data()
	{
		/**************** Settings begin **************/
		// See http://www.interfax.net/en/dev/webservice/reference/faxquery2
		// for an explanation of properties

		// Valid values for [property]_verb are:
		// Equals | GreaterThan | GreaterOrEqual | LessThan | LessOrEqual | Like | IncludedIn | Between
		// Leave both [property]_verb and [property]_data empty to ignore the property in the query
		$account = $this->account_inuse();
		$username = $account->username;
		$password = $account->password;

		$subject_verb = '';
		$subject_data = '';

		$faxnumber_verb = '';
		$faxnumber_data = '';

		$date_from = '';
		$date_to = '';

		$user_id_verb = '';
		$user_id_data = '';

		$reply_address_verb = '';
		$reply_address_data = '';

		$transaction_id_verb = '';
		$transaction_id_data = '';

		$parent_transaction_id_verb = '';
		$parent_transaction_id_data = '';

		$status_verb = '';
		$status_data = '';

		// The following values control display settings

		$show_hidden_transactions = TRUE;
		$only_parents = false;
		$num_of_results = 100;
		$starting_record = 0;

		// Valid values for $order_by are:
		// TransactionID | SubmitTime | CompletionTime | Status | DestinationFax | Subject | PagesSent | UserID | ReplyEmail

		$order_by = 'TransactionID';
		$asc_order_direction = FALSE;
		$return_items = TRUE;
		$return_stats = TRUE;

		/**************** Settings end **************/

		// Set default values for empty query properties

		if(!($subject_verb && $subject_data)){
			$subject_verb = 'Equals';
			$subject_data = '';
		}

		if(!($faxnumber_verb && $faxnumber_data)){
			$faxnumber_verb = 'Equals';
			$faxnumber_data = '';
		}

		if(!$date_from) $date_from = '1999-01-01T00:00:01';

		if(!$date_to) $date_to = '2999-12-31T23:59:59';

		if(!($user_id_verb && $user_id_data)){
			$user_id_verb = 'Equals';
			$user_id_data = '';
		}

		if(!($reply_address_verb && $reply_address_data)){
			$reply_address_verb = 'Equals';
			$reply_address_data = '';
		}

		if(!($transaction_id_verb && $transaction_id_data)){
			$transaction_id_verb = 'Equals';
			$transaction_id_data = '';
		}

		if(!($parent_transaction_id_verb && $parent_transaction_id_data)){
			$parent_transaction_id_verb = 'Equals';
			$parent_transaction_id_data = '';
		}

		if(!($status_verb && !is_null($status_data))){
			$status_verb = 'Equals';
			$status_data = '';
		}

		// End setting default values
		$params = new stdClass();
		$params->QueryForm = new stdClass();
		$params->QueryForm->Subject = new stdClass();
		$params->QueryForm->FaxNumber = new stdClass();
		$params->QueryForm->UserID = new stdClass();
		$params->QueryForm->ReplyAddress = new stdClass();
		$params->QueryForm->TransactionID = new stdClass();
		$params->QueryForm->ParentTransactionID = new stdClass();
		$params->QueryForm->Status = new stdClass();
		$params->QueryControl = new stdClass();
		$params->Username                                 = $username;
		$params->Password                                 = $password;
		$params->QueryForm->Subject->Verb                 = $subject_verb;
		$params->QueryForm->Subject->VerbData             = $subject_data;
		$params->QueryForm->FaxNumber->Verb               = $faxnumber_verb;
		$params->QueryForm->FaxNumber->VerbData           = $faxnumber_data;
		$params->QueryForm->DateFrom                      = $date_from;
		$params->QueryForm->DateTo                        = $date_to;
		$params->QueryForm->UserID->Verb                  = $user_id_verb;
		$params->QueryForm->UserID->VerbData              = $user_id_data;
		$params->QueryForm->ReplyAddress->Verb            = $reply_address_verb;
		$params->QueryForm->ReplyAddress->VerbData        = $reply_address_data;
		$params->QueryForm->TransactionID->Verb           = $transaction_id_verb;
		$params->QueryForm->TransactionID->VerbData       = $transaction_id_data;
		$params->QueryForm->ParentTransactionID->Verb     = $parent_transaction_id_verb;
		$params->QueryForm->ParentTransactionID->VerbData = $parent_transaction_id_data;
		$params->QueryForm->Status->Verb                  = $status_verb;
		$params->QueryForm->Status->VerbData              = $status_data;
		$params->QueryForm->ShowHiddenTransactions        = $show_hidden_transactions;
		$params->QueryControl->OnlyParents                = $only_parents;
		$params->QueryControl->NumOfResults               = $num_of_results;
		$params->QueryControl->StartingRecord             = $starting_record;
		$params->QueryControl->OrderBy                    = $order_by;
		$params->QueryControl->AscOrderDirection          = $asc_order_direction;
		$params->QueryControl->ReturnItems                = $return_items;
		$params->QueryControl->ReturnStats                = $return_stats;


		$client = new SoapClient("http://ws.interfax.net/dfs.asmx?wsdl");

		// Use the call below to trace PHP's SOAP call
		// $client = new SoapClient("http://ws.interfax.net/dfs.asmx?wsdl", array('trace' => 1));

		return $queryResult = $client->FaxQuery2($params);

	}



	private function account_inuse()
	{
		return  $account = fax_account::find_by_status('inuse');
			
	}

}